#include "aws.h"



AWS_Interface_S AWS_Interface;


static const char *TAG = "AWS IoT客户端";

const char *rootCA;
const char *deviceCert;
const char *devicePrivateKey;

static char public_TOPIC[30] = "heartbeat/123456789012";     //心跳topic
static char public_result_TOPIC[30] = "result/123456789012"; //指令反馈topic
static char public_alert_TOPIC[30] = "alert/123456789012";   //报警topic
static char subscribe_TOPIC[30] = "command/123456789012";    //订阅指令topic
static int public_TOPIC_LEN;
static int public_result_TOPIC_LEN;
static int public_alert_TOPIC_LEN;
static int subscribe_TOPIC_LEN;


extern volatile unsigned int ID_first_four; //MAC初4位做识别
extern volatile unsigned int ID_mid_four;   //MAC中间4位做识别
extern volatile unsigned int ID_last_four;  //MAC最后4位做识别

TaskHandle_t aws_send_xHandle = NULL;    //任务句柄
TaskHandle_t aws_receive_xHandle = NULL; //任务句柄

//美国服务器的证书
const char root[] = {"-----BEGIN CERTIFICATE-----\n\
MIIDQTCCAimgAwIBAgITBmyfz5m/jAo54vB4ikPmljZbyjANBgkqhkiG9w0BAQsF\n\
ADA5MQswCQYDVQQGEwJVUzEPMA0GA1UEChMGQW1hem9uMRkwFwYDVQQDExBBbWF6\n\
b24gUm9vdCBDQSAxMB4XDTE1MDUyNjAwMDAwMFoXDTM4MDExNzAwMDAwMFowOTEL\n\
MAkGA1UEBhMCVVMxDzANBgNVBAoTBkFtYXpvbjEZMBcGA1UEAxMQQW1hem9uIFJv\n\
b3QgQ0EgMTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALJ4gHHKeNXj\n\
ca9HgFB0fW7Y14h29Jlo91ghYPl0hAEvrAIthtOgQ3pOsqTQNroBvo3bSMgHFzZM\n\
9O6II8c+6zf1tRn4SWiw3te5djgdYZ6k/oI2peVKVuRF4fn9tBb6dNqcmzU5L/qw\n\
IFAGbHrQgLKm+a/sRxmPUDgH3KKHOVj4utWp+UhnMJbulHheb4mjUcAwhmahRWa6\n\
VOujw5H5SNz/0egwLX0tdHA114gk957EWW67c4cX8jJGKLhD+rcdqsq08p8kDi1L\n\
93FcXmn/6pUCyziKrlA4b9v7LWIbxcceVOF34GfID5yHI9Y/QCB/IIDEgEw+OyQm\n\
jgSubJrIqg0CAwEAAaNCMEAwDwYDVR0TAQH/BAUwAwEB/zAOBgNVHQ8BAf8EBAMC\n\
AYYwHQYDVR0OBBYEFIQYzIU07LwMlJQuCFmcx7IQTgoIMA0GCSqGSIb3DQEBCwUA\n\
A4IBAQCY8jdaQZChGsV2USggNiMOruYou6r4lK5IpDB/G/wkjUu0yKGX9rbxenDI\n\
U5PMCCjjmCXPI6T53iHTfIUJrU6adTrCC2qJeHZERxhlbI1Bjjt/msv0tadQ1wUs\n\
N+gDS63pYaACbvXy8MWy7Vu33PqUXHeeE6V/Uq2V8viTO96LXFvKWlJbYK8U90vv\n\
o/ufQJVtMVT8QtPHRh8jrdkPSHCa2XV4cdFyQzR1bldZwgJcJmApzyMZFo6IQ6XU\n\
5MsI+yMRQ+hDKXJioaldXgjUkK642M4UwtBV8ob2xJNDd2ZhwLnoQdeXeGADbkpy\n\
rqXRfboQnoZsG4q5WTP468SQvvG5\n\
-----END CERTIFICATE-----\n"};

const char aws_ca_cert[] = {"-----BEGIN CERTIFICATE-----\n\
MIIDWTCCAkGgAwIBAgIUMVjRE3gQCLlDcZTQu/SK0YfiUCAwDQYJKoZIhvcNAQEL\n\
BQAwTTFLMEkGA1UECwxCQW1hem9uIFdlYiBTZXJ2aWNlcyBPPUFtYXpvbi5jb20g\n\
SW5jLiBMPVNlYXR0bGUgU1Q9V2FzaGluZ3RvbiBDPVVTMB4XDTIwMDYwOTA5NDQz\n\
OFoXDTQ5MTIzMTIzNTk1OVowHjEcMBoGA1UEAwwTQVdTIElvVCBDZXJ0aWZpY2F0\n\
ZTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAJzVkxmN36pqilYKU0Bg\n\
zCvLyuNeLQdhAnmbd2gEb1r8vufLuC6g1/w6KH034PK98bGNkl8IVuM6eUWM1LMI\n\
mqd+Pd5xpOpkohm2QN5GOymFMawW0nEdfTak2NAVbBqT1gowfR++VICJPouE2F22\n\
2smsdovhb/5CtAyRN4QabIQ9va6MW3jglJ+Sf71bwD0tVf5/yxMuaB6ijpoHH270\n\
X/1KLPS88qdTairfJaPXOdQilAQsjjP+7I64O4dagkybtt3aDZp2GLMWnDqCVQxl\n\
ifgNHD/NvgopoKu0EqVBQ10ygWq3YolQTTx5NrA0Gi9zhauRLR/k4cPMHSj8sbLL\n\
rL8CAwEAAaNgMF4wHwYDVR0jBBgwFoAUYidCrrUorrssOdICSyYCAtgCCLowHQYD\n\
VR0OBBYEFDx4NnA9k9D66PRCAPuDcEMwYMADMAwGA1UdEwEB/wQCMAAwDgYDVR0P\n\
AQH/BAQDAgeAMA0GCSqGSIb3DQEBCwUAA4IBAQCKVMg2w7LPMueyXr+mDFPFBhad\n\
NlHC10cfh4N5Zr979fvY9/DczMQBTz/vG27n4Sd6h9VxcXM08WflJFa+8cM2muhk\n\
1gEA+t0feLV/HMWn9fEMeOw9eostOZt73gXt4zGBAuR1QrWbUodBY/1i1Q0Ps0B0\n\
KvZSG1eJ5zCS9rGU0IuAttBIHdIFofi68KyD0Goe8WNuN/O3XCCfiD9RQFPXSABA\n\
G1ckG3fUbNyz9y9N5+2F/cfySf4PAGnOL95i76Yjw8XDEpPaqe2a/rTxlmOmVKLc\n\
FyD7O+IOM4+CN3QFpNCxL34YlWUiR+Jt3Cmmp5gGJjVzhfMjLRqoN7Le6ppg\n\
-----END CERTIFICATE-----\n"};

const char aws_private_cert[] = {"-----BEGIN RSA PRIVATE KEY-----\n\
MIIEpAIBAAKCAQEAnNWTGY3fqmqKVgpTQGDMK8vK414tB2ECeZt3aARvWvy+58u4\n\
LqDX/DoofTfg8r3xsY2SXwhW4zp5RYzUswiap3493nGk6mSiGbZA3kY7KYUxrBbS\n\
cR19NqTY0BVsGpPWCjB9H75UgIk+i4TYXbbayax2i+Fv/kK0DJE3hBpshD29roxb\n\
eOCUn5J/vVvAPS1V/n/LEy5oHqKOmgcfbvRf/Uos9Lzyp1NqKt8lo9c51CKUBCyO\n\
M/7sjrg7h1qCTJu23doNmnYYsxacOoJVDGWJ+A0cP82+Cimgq7QSpUFDXTKBardi\n\
iVBNPHk2sDQaL3OFq5EtH+Thw8wdKPyxssusvwIDAQABAoIBAFumS3IRgHU8EV86\n\
ZXAnoOivDP9Vl4bKYAHkoiC9Br3rGTynxhgpkk4pRmP2dZQk/8q8ap7yZqQfFaNZ\n\
Kyr8ijP7B/in/LGk4T2ZTcsWy5f16BMiuxOzIer+5lyjSCs33UrTDyuvs/3xNgWe\n\
Ojp5kt1r1VLlCOdS35jAe+rM7kFQUP21OrA8qZtXEvXa7roWJyGiqYLBJ4ZavMvk\n\
AkLxxWiloKrgdZidM8NUsvREU5WXGkuYmeqBEFVE12AUc31XsOYadwLFRkTr1xlb\n\
7cL1Qom4vYYTIgYw/5IgIVuoA+kwVsVS1LWEkXWEk3FO7k3dWyQSlvXoWekbXI12\n\
QaYhLYECgYEAzHf/ewXu74xpu2OvOuBFa+YeUEySqGSUBCUfpuHXd9GLyTHdWS6V\n\
JOJrWwPK6RW197C7RacEfFQB+nXy1D7f4xn22p5B0airUWLZHzStYF7NQixVOqyo\n\
hV3pvlhCBdD73BIHp8ZbzDAAIQD7jjOwBILKvibwc37GFaxnK5oQJd8CgYEAxFxG\n\
lKoihbmR8Av8/1w0cWOs7Pn0JFE3cRrpwYZ+wEnv31N6CIGPtFWeFq/9E29UX6q1\n\
VYZCtGvtnDlWwgBX9df/nSb7i+pp0ldMwdysIkeA9pKvLX0CArlzn1dtzbxretX0\n\
hoAaMpz2lcI+BGsFf/JP8EJeI7A/HdILsmzUlSECgYEAkhOk5Vpe0qtJGoRkOzbg\n\
we5cd9fM6xs4GCIBk/NDQanmKS5DyiQaj4hnVFXIUG+fWj7b/C4PaVeHNk2v12y3\n\
/QKetp9bvJK5GmL5ZW738qKoF/+eQFX765Qlm0RonskoNph2+akgCie839dY4MYS\n\
mZZrjCA7pvfKQs+xwZfGs5cCgYAyYv9VjKbZK7owH9Nm75Ehe5NXJ/inDRQt+Nr8\n\
5vdFfnOIGhKZdi2a99FePwCw6QN8nQB1FsOtKnjJeskxdQcdNbUI7YTk7V0ENZSj\n\
tm2RSknke9Vp1ccbTd1GiRfLEEuANMDPdrXCQRbhbPufBJtfrp0mqNEhVUuM6mZH\n\
/bc7IQKBgQCwAR9OnWCY8U9OkAdiSQzjKqlKsS4Tc4gnw5cMY31I3NtPx7u+ZHL1\n\
7qtOKenAKIJNlS5TrdT37mdZdT2xn5Ko5SSQmt/SrW7xep/X57juho7KjJNGzrYK\n\
QVhqV/CWK/Hw4zwkoiCOL4HzGJ0FkSg2EPcdkWlvoKCq84PLqRO40w==\n\
-----END RSA PRIVATE KEY-----\n"};

char cPayload[500];
int32_t i = 0;
IoT_Error_t rc = FAILURE;
IoT_Error_t ping = FAILURE;
AWS_IoT_Client client;
IoT_Publish_Message_Params paramsQOS0; //AWS MQTT消息变量
IoT_Publish_Message_Params paramsQOS1; //AWS MQTT 消息变量
char HostAddress[255] = AWS_IOT_MQTT_HOST;
unsigned char received_command[200];
unsigned int received_command_length = 0;
static unsigned int send_result = 0;
static char command_id[50];
static signed char aws_counter = 0;
uint32_t port = AWS_IOT_MQTT_PORT;

SemaphoreHandle_t sync_aws_creat;

void disconnectCallbackHandler(AWS_IoT_Client *pClient, void *data);                                                                                   //AWS断开事件
void iot_subscribe_callback_handler(AWS_IoT_Client *pClient, char *topicName, uint16_t topicNameLen, IoT_Publish_Message_Params *params, void *pData); //收到信息事件处理
void aws_send(void *param);                                                                                                                            //aws上报数据
void aws_receive(void *param);                                                                                                                         //aws接收数据


//aws连接
esp_err_t AWS_connect(void)
{
    IoT_Client_Init_Params mqttInitParams;
    IoT_Client_Connect_Params connectParams;
    IoT_MQTT_Will_Options willParams;

    ESP_LOGE(TAG, "开始连接到AWS IoT...");
    //AWS连接变量配置
    rootCA = (const char *)root;
    deviceCert = (const char *)aws_ca_cert;
    devicePrivateKey = (const char *)aws_private_cert;

    mqttInitParams = iotClientInitParamsDefault;
    mqttInitParams.pHostURL = HostAddress;
    mqttInitParams.port = port;
    mqttInitParams.pRootCALocation = (const char *)rootCA;
    mqttInitParams.pDeviceCertLocation = (const char *)deviceCert;
    mqttInitParams.pDevicePrivateKeyLocation = (const char *)devicePrivateKey;
    // mqttInitParams.mqttCommandTimeout_ms = 400;
    // mqttInitParams.tlsHandshakeTimeout_ms = 3000;
    mqttInitParams.disconnectHandler = disconnectCallbackHandler;
    rc = aws_iot_mqtt_init(&client, &mqttInitParams);
    if (SUCCESS != rc)
    {
        // client = 0;
        ESP_LOGE(TAG, "AWS 客户端初始化失败, Error: %d", rc);
        return ESP_OK;
    }
    ESP_LOGE(TAG, "AWS IoT客户端初始化...OK");

    connectParams = iotClientConnectParamsDefault;
    connectParams.pClientID = public_TOPIC; //配置client id
    connectParams.clientIDLen = (uint16_t)strlen(public_TOPIC); //设置client id 长度
    connectParams.keepAliveIntervalInSec = 10; //设置遗愿触发时间 如果是手动杀掉会立刻触发遗言消息，而如果是断网，设备掉电，则会更根据这个属性来触发遗言消息
    // connectParams.isCleanSession = true; //会话重用机制
    willParams = iotMqttWillOptionsDefault;
    willParams.pTopicName = public_TOPIC;
    willParams.topicNameLen = (uint16_t)strlen(public_TOPIC);
    char* msg = "The controller is offline";
    willParams.pMessage = msg;
    willParams.msgLen = (uint16_t)strlen(msg);
    willParams.qos = QOS1;
    connectParams.isWillMsgPresent = true; //设置遗愿 包括遗愿主题、遗愿 QoS、遗愿消息等
    connectParams.will = willParams;
    rc = aws_iot_mqtt_connect(&client, &connectParams);
    if (SUCCESS != rc)
    { //很容易出现 握手失败
        ESP_LOGE(TAG, "连接到AWS IoT Error(%d) ClientState:%d", rc, client.clientStatus.clientState);
        if(AWS_Interface.Set_Alot_State != NULL) AWS_Interface.Set_Alot_State(Alot_AWS_Disconn);
        return ESP_OK;
    }
    ESP_LOGI(TAG, "已连接到AWS IoT, 开始订阅command主题...");
    rc = aws_iot_mqtt_subscribe(&client, subscribe_TOPIC, subscribe_TOPIC_LEN, QOS0, iot_subscribe_callback_handler, NULL);
    if (SUCCESS != rc)
    { //握手超时
        ESP_LOGE(TAG, "Error subscribing : %d", rc);
        return ESP_OK;
    }
    ESP_LOGI(TAG, "订阅主题...OK");

    wireless_state.aws_connect_state = connected;
    UI_System.OTA_Check = UI_ON;

    return ESP_OK;


    //MQTT 消息载体配置
    //QOS 0至多一次 无需客户端确认，其可靠性与基础网络层 TCP/IP 一致
    //QOS 1至少一次，有可能重复 确保至少向客户端发送一次信息，不过也可发送多次；在接收数据包时，需要客户端返回确认消息（ACK 包）。这种方式常用于传递确保交付的信息，但开发人员必须确保其系统可以处理重复的数据包
    sprintf(cPayload, "%s : %d ", "hello from SDK", i);
    //0级消息
    paramsQOS0.qos = QOS0;
    paramsQOS0.payload = (void *)cPayload;
    paramsQOS0.isRetained = 0;
    //1级消息
    paramsQOS1.qos = QOS1;
    paramsQOS1.payload = (void *)cPayload;
    paramsQOS1.isRetained = 0;
    if (aws_send_xHandle == NULL)
    {
        ESP_LOGI(TAG, "creat aws send task");
        xTaskCreate(aws_send, "aws_send", 3 * 1024, NULL, 10, &aws_send_xHandle);
        configASSERT(aws_send_xHandle);
    }

    //配置自动重连
    // rc = aws_iot_mqtt_autoreconnect_set_status(&client, true);
    // if (SUCCESS != rc)
    // {
    //     ESP_LOGE(TAG, "无法设置AWS自动重连 - %d", rc);
    //     return ESP_OK;
    // }

    return ESP_OK;
}

//建立aws连接任务
void aws_creat_task(void *param)
{
    ESP_LOGE(TAG, "等待WIFI连接成功...");
    xSemaphoreTake(sync_aws_creat, portMAX_DELAY);
    //开始aws连接
    AWS_connect();

    while (1)
    {
        //运行中手动进入更新
        if ((aws_counter == 0) || (aws_counter > 5))
        {
            //更新失败后重新建立任务
            if (aws_send_xHandle == NULL)
            {
                ESP_LOGI(TAG, "Re-Creat AWS send task");
                xTaskCreate(aws_send, "aws_send", 3 * 1024, NULL, 10, &aws_send_xHandle);
                configASSERT(aws_send_xHandle);
                xTaskCreate(aws_receive, "aws_receive", 2 * 1024, NULL, 10, &aws_receive_xHandle);
                configASSERT(aws_receive_xHandle);
            }
            ping = aws_iot_mqtt_yield(&client, 1);
            //断开连接 进入自动重连
            if (ping != 0)
            {
                wireless_state.aws_connect_state = disconnect;
                rc = aws_iot_mqtt_attempt_reconnect(&client);
                // ESP_LOGI(TAG, "reconnect result is %d", (int)rc);
                aws_counter = -1;
            }
            else
            {
                wireless_state.aws_connect_state = connected;
            }
        }
        aws_counter++;
        // ESP_LOGI(TAG, "'%s' is %d bytes", pcTaskGetTaskName(NULL), uxTaskGetStackHighWaterMark(NULL));
        vTaskDelay(500 / portTICK_RATE_MS);
    }
}

//aws初始化
esp_err_t AWS_Init(void)
{
    //生成aws的topic
    extern volatile unsigned int ID_first_four; //MAC初4位做识别
    extern volatile unsigned int ID_mid_four;   //MAC中间4位做识别
    extern volatile unsigned int ID_last_four;  //MAC最后4位做识别
    uint8_t i = 0;

    for(i = 0; i < 30; i++)
    {
        public_TOPIC[i] = 0;
        public_result_TOPIC[i] = 0;
        public_alert_TOPIC[i] = 0;
        subscribe_TOPIC[i] = 0;
    }
    sprintf(public_TOPIC,"heartbeat/%04x%04x%04x",ID_first_four,ID_mid_four,ID_last_four);
    sprintf(subscribe_TOPIC,"command/%04x%04x%04x",ID_first_four,ID_mid_four,ID_last_four);
    sprintf(public_alert_TOPIC,"alert/%04x%04x%04x",ID_first_four,ID_mid_four,ID_last_four);
    sprintf(public_result_TOPIC,"result/%04x%04x%04x",ID_first_four,ID_mid_four,ID_last_four);
    public_TOPIC_LEN = strlen(public_TOPIC);
    public_alert_TOPIC_LEN = strlen(public_alert_TOPIC);
    public_result_TOPIC_LEN = strlen(public_result_TOPIC);
    subscribe_TOPIC_LEN = strlen(subscribe_TOPIC);

    sync_aws_creat = xSemaphoreCreateBinary();
    xTaskCreate(aws_creat_task, "aws_creat_task", 8 * 1024, NULL, 11, NULL); //确认最小
    xTaskCreate(aws_receive, "aws_receive", 2 * 1024, NULL, 10, &aws_receive_xHandle);

    return ESP_OK;
}

//aws断开事件处理
void disconnectCallbackHandler(AWS_IoT_Client *pClient, void *data)
{
    if(AWS_Interface.Set_Alot_State != NULL) AWS_Interface.Set_Alot_State(Alot_AWS_Disconn);
    ESP_LOGW(TAG, "AWS Client Disconnect");
    IoT_Error_t rc = FAILURE;
    if (NULL == pClient)
    {
        return;
    }
    if (aws_iot_is_autoreconnect_enabled(pClient))
    {
        ESP_LOGI(TAG, "Auto Reconnect is enabled, Reconnecting attempt will start now");
        if(AWS_Interface.Set_Alot_State != NULL) AWS_Interface.Set_Alot_State(Alot_AWS_Reconn);
    }
    else
    {
        ESP_LOGW(TAG, "Auto Reconnect not enabled. Starting manual reconnect...");
        if(AWS_Interface.Set_Alot_State != NULL) AWS_Interface.Set_Alot_State(Alot_AWS_Reconn);
        rc = aws_iot_mqtt_attempt_reconnect(pClient);
        if (NETWORK_RECONNECTED == rc)
        {
            ESP_LOGW(TAG, "Manual Reconnect Successful");
        }
        else
        {
            ESP_LOGW(TAG, "Manual Reconnect Failed - %d", rc);
        }
    }
}

//aws收到事件
void iot_subscribe_callback_handler(AWS_IoT_Client *pClient, char *topicName, uint16_t topicNameLen, IoT_Publish_Message_Params *params, void *pData)
{
    // ESP_LOGI(TAG, "%.*s\t%.*s", topicNameLen, topicName, (int)params->payloadLen, (char *)params->payload);
    received_command_length = (unsigned int)params->payloadLen;
    for (int i = 0; i < 200; i++)
    {
        received_command[i] = '\0';
        if(i < received_command_length) received_command[i] = ((uint8_t*)(params->payload))[i];
    }
    // memcpy(received_command, params->payload, received_command_length);
}

//aws 上报数据
void aws_send(void *param)
{
    extern volatile char heatrtbeat[500];                  //心跳
    extern const unsigned char grill_model;                //烤炉型号
    extern volatile unsigned char error_state;             //报错代码
    extern volatile unsigned char global_power_switch;     //电源开关
    extern volatile unsigned char global_heat_switch;      //加热开关
    extern volatile unsigned char global_grill_state;      //烤炉状态
    extern volatile unsigned int global_set_temp;          //设置温度
    extern volatile signed int global_rtd0_temp;           //RTD温度
    extern volatile unsigned int global_rtd1_temp;         //RTD1温度
    extern volatile unsigned int global_rtd2_temp;         //RTD2温度
    extern volatile unsigned int global_rtd1_set_temp;     //RTD1报警温度
    extern volatile unsigned int global_rtd2_set_temp;     //RTD2报警温度
    extern volatile unsigned long global_timmer_1_set;     //定时器1设定值
    extern volatile unsigned long global_timmer_2_set;     //定时器2设定值
    extern volatile unsigned long global_countdown_remain; //熄火程序剩余时间
    extern volatile unsigned long global_timmer_1_remain;  //定时器1剩余
    extern volatile unsigned long global_timmer_2_remain;  //定时器2剩余
    extern volatile unsigned int ID_first_four;            //MAC初4位做识别
    extern volatile unsigned int ID_mid_four;              //MAC中间4位做识别
    extern volatile unsigned int ID_last_four;             //MAC最后4位做识别
    extern volatile unsigned int firmware_number;          //固件版本号
    extern volatile unsigned char global_temp_unit;        //温度单位 0华氏度 1摄氏度f
    extern volatile unsigned char timmer_1_name[100];      //定时器1名称
    extern volatile unsigned char timmer_2_name[100];      //定时器2名称
    
    extern volatile char heatrtbeat[500];            //心跳
    extern volatile unsigned int ID_first_four;      //MAC初4位做识别
    extern volatile unsigned int ID_mid_four;        //MAC中间4位做识别
    extern volatile unsigned int ID_last_four;       //MAC最后4位做识别
    extern volatile unsigned char global_tmr1_alert; //定时器1报警
    extern volatile unsigned char global_tmr2_alert; //定时器2报警
    extern volatile unsigned char global_rtd1_alert; //肉探针1报警
    extern volatile unsigned char global_rtd2_alert; //肉探针2报警
    unsigned char alert_type = 0;
    static unsigned char last_tmr1_alert = 0;
    static unsigned char last_tmr2_alert = 0;
    static unsigned char last_rtd1_alert = 0;
    static unsigned char last_rtd2_alert = 0;

    while (1)
    {
        if ((aws_counter > 1) && (ping == 0))
        {
            // rc = aws_iot_mqtt_yield(&client, 1);
            // if (rc != 0)
            // {
            //     // If the client is attempting to reconnect we will skip the rest of the loop.
            //     ESP_LOGI(TAG, "[warnning]aws connect error%d", (int)rc);
            //     vTaskDelay(1000 / portTICK_RATE_MS);
            //     continue;
            // }

            if(AWS_Interface.Get_RTD_Handle != NULL) global_rtd0_temp = AWS_Interface.Get_RTD_Handle();
            if(AWS_Interface.Get_ProbeA_Handle != NULL) global_rtd1_temp = AWS_Interface.Get_ProbeA_Handle();
            if(AWS_Interface.Get_ProbeB_Handle != NULL) global_rtd2_temp = AWS_Interface.Get_ProbeB_Handle();
            if(AWS_Interface.Get_RTDSettings_Handle != NULL) global_set_temp = AWS_Interface.Get_RTDSettings_Handle();
            if(AWS_Interface.Get_ProbeASettings_Handle != NULL) global_rtd1_set_temp = AWS_Interface.Get_ProbeASettings_Handle();
            if(AWS_Interface.Get_ProbeBSettings_Handle != NULL) global_rtd2_set_temp = AWS_Interface.Get_ProbeBSettings_Handle();
            if(AWS_Interface.Get_Timer1Set_Handle != NULL) global_timmer_1_set = AWS_Interface.Get_Timer1Set_Handle();
            // if(AWS_Interface.Get_Timer2Set_Handle != NULL) global_timmer_2_set = AWS_Interface.Get_Timer2Set_Handle();
            if(AWS_Interface.Get_Timer1_Handle != NULL)
            {
                global_timmer_1_remain = AWS_Interface.Get_Timer1_Handle();
                global_countdown_remain = global_timmer_1_remain;
            }
            // if(AWS_Interface.Get_Timer2_Handle != NULL) global_timmer_2_remain = AWS_Interface.Get_Timer2_Handle();
            if(AWS_Interface.Get_Timer1_Alert != NULL) global_tmr1_alert = AWS_Interface.Get_Timer1_Alert();
            // if(AWS_Interface.Get_Timer2_Alert != NULL) global_tmr2_alert = AWS_Interface.Get_Timer2_Alert();
            if(AWS_Interface.Get_ProbeA_Alert != NULL) global_rtd1_alert = AWS_Interface.Get_ProbeA_Alert();
            if(AWS_Interface.Get_ProbeB_Alert != NULL) global_rtd2_alert = AWS_Interface.Get_ProbeB_Alert();
            if(AWS_Interface.Get_State_Error != NULL) error_state = AWS_Interface.Get_State_Error();
            if(AWS_Interface.Get_State_heat != NULL) global_heat_switch = AWS_Interface.Get_State_heat();
            if(AWS_Interface.Get_State_Grill != NULL) global_grill_state = AWS_Interface.Get_State_Grill();
            if(AWS_Interface.Get_Unit_Grill != NULL) global_temp_unit = AWS_Interface.Get_Unit_Grill();
            
            //ESP_LOGI(TAG, "'%s' is %d bytes", pcTaskGetTaskName(NULL), uxTaskGetStackHighWaterMark(NULL));
            //发送心跳数据
            // sprintf(heatrtbeat, "{\"ID\":\"%x%x%x\",\"firmware\":%d,\"is_pwr\":%d,\"is_heat\":%d,\"state\":%d,\"countdown\":%ld,\"rtd0_set\":%d,\"rtd1_set\":%d,\"rtd2_set\":%d,\"rtd0_temp\":%d,\"rtd1_temp\":%d,\"rtd2_temp\":%d,\"tmr1_set\":%ld,\"tmr2_set\":%ld,\"tmr1_rem\":%ld,\"tmr2_rem\":%ld,\"tmr1 name\":\"%s\",\"tmr2 name\":\"%s\",\"unit\":%d,\"err\":%d,\"ssid\":\"%s\",\"squ\":%d}", ID_first_four, ID_mid_four, ID_last_four, firmware_number, global_power_switch, global_heat_switch, global_grill_state, global_countdown_remain, global_set_temp, global_rtd1_set_temp, global_rtd2_set_temp, global_rtd0_temp, global_rtd1_temp, global_rtd2_temp, global_timmer_1_set, global_timmer_2_set, global_timmer_1_remain, global_timmer_2_remain, timmer_1_name, timmer_2_name, global_temp_unit, error_state, g_wifi_ssid, i++);
            // memcpy(cPayload, heatrtbeat, 500); //复制command id
            for (int i = 0; i < 500; i++)
            {
                cPayload[i] = heatrtbeat[i];
            }
            
            paramsQOS1.payloadLen = strlen(cPayload);
            rc = aws_iot_mqtt_publish(&client, public_TOPIC, public_TOPIC_LEN, &paramsQOS1);
            // ESP_LOGI(TAG, "send message to aws %s result is %d", cPayload, (int)rc);
            //判断报警状态
            //只报警一次
            //查询报警类别 生成报警值
            if (global_tmr1_alert || global_tmr2_alert || global_rtd1_alert || global_rtd2_alert) //判断有报警值触发
            {
                alert_type = 0;
                if ((global_tmr1_alert == 1) && (last_tmr1_alert == 0) && (alert_type == 0)) //上一次无报警值 这次有报警值 报警并记录
                {
                    last_tmr1_alert = 1;
                    alert_type = 1;
                }
                else if (global_tmr1_alert == 0) //复位报警
                {
                    last_tmr1_alert = 0;
                }
                if ((global_tmr2_alert == 1) && (last_tmr2_alert == 0) && (alert_type == 0))
                {
                    last_tmr2_alert = 1;
                    alert_type = 2;
                }
                else if (global_tmr2_alert == 0)
                {
                    last_tmr2_alert = 0;
                }
                if ((global_rtd1_alert == 1) && (last_rtd1_alert == 0) && (alert_type == 0))
                {
                    last_rtd1_alert = 1;
                    alert_type = 3;
                }
                else if (global_rtd1_alert == 0)
                {
                    last_rtd1_alert = 0;
                }
                if ((global_rtd2_alert == 1) && (last_rtd2_alert == 0) && (alert_type == 0))
                {
                    last_rtd2_alert = 1;
                    alert_type = 4;
                }
                else if (global_rtd2_alert == 0)
                {
                    last_rtd2_alert = 0;
                }
                //发送报警信息
                if (alert_type != 0)
                {
                    sprintf(cPayload, "{\"ID\":\"%04x%04x%04x\",\"alert_type\":%d}", ID_first_four, ID_mid_four, ID_last_four, alert_type);
                    paramsQOS0.payloadLen = strlen(cPayload);
                    rc = aws_iot_mqtt_publish(&client, public_alert_TOPIC, public_alert_TOPIC_LEN, &paramsQOS0);
                    ESP_LOGI(TAG, "send alert message to aws %s", cPayload);
                }
            }
            //无报警值
            else
            {
                last_tmr1_alert = 0;
                last_tmr2_alert = 0;
                last_rtd1_alert = 0;
                last_rtd2_alert = 0;
            }

            //数据接收反馈
            // if (send_result)
            // {
            //     //指令执行完毕
            //     send_result = 0;
            //     int i = 1;
            //     sprintf(cPayload, "{\"ID\":\"%04x%04x%04x\",\"command_id\":%s,\"result\":%d}", ID_first_four, ID_mid_four, ID_last_four, command_id, i);
            //     paramsQOS0.qos = QOS0;
            //     paramsQOS0.payload = (void *)cPayload;
            //     paramsQOS0.isRetained = 0;
            //     paramsQOS0.payloadLen = strlen(cPayload);
            //     rc = aws_iot_mqtt_publish(&client, public_result_TOPIC, public_result_TOPIC_LEN, &paramsQOS0);
            //     ESP_LOGI(TAG, "send result message to aws result topic %s", cPayload);
            // }
            aws_counter = 0;
            if(AWS_Interface.Set_Alot_State != NULL) AWS_Interface.Set_Alot_State(Alot_AWS_Connected);
        }
        vTaskDelay(400 / portTICK_RATE_MS); //任务延迟
        // ESP_LOGI(TAG, "ID\":\"%x%x%x\",\"firmware\":%d",ID_first_four, ID_mid_four, ID_last_four, firmware_number);
    }
}

//接收蓝牙/WIFI数据后 解码
void DataRx_Decode(void)
{
    unsigned char len = 0;
    char dest[100];                             //临时变量
    extern volatile unsigned int ID_first_four; //MAC初4位做识别
    extern volatile unsigned int ID_mid_four;   //MAC中间4位做识别
    extern volatile unsigned int ID_last_four;  //MAC最后4位做识别

    extern volatile unsigned char global_power_switch_command; //电源开关
    extern volatile unsigned char global_heat_switch_command;  //加热开关
    extern volatile unsigned int global_set_temp_command;      //设置温度
    extern volatile unsigned int global_rtd1_set_temp_command; //RTD1报警温度
    extern volatile unsigned int global_rtd2_set_temp_command; //RTD2报警温度
    extern volatile unsigned long global_timmer_0_set_command; //定时器0设定值 用于设置菜单的运行时间
    extern volatile unsigned long global_timmer_1_set_command; //定时器1设定值
    extern volatile unsigned long global_timmer_2_set_command; //定时器2设定值
    extern volatile unsigned char global_temp_unit_command;    //温度单位 0华氏度 1摄氏度
    extern volatile unsigned int firmware_number;              //固件版本号

    extern volatile unsigned char global_power_switch;            //电源开关
    extern volatile unsigned char global_heat_switch;             //加热开关
    extern volatile unsigned char global_cancel_shutdown_command; //取消熄火
    extern volatile unsigned int global_set_temp;                 //设置温度
    extern volatile unsigned int global_rtd1_set_temp;            //RTD1报警温度
    extern volatile unsigned int global_rtd2_set_temp;            //RTD2报警温度
    extern volatile unsigned long global_timmer_0_set;            //定时器0设定值 用于设置菜单的运行时间
    extern volatile unsigned long global_timmer_1_set;            //定时器1设定值
    extern volatile unsigned long global_timmer_2_set;            //定时器2设定值
    extern volatile unsigned char global_temp_unit;               //温度单位 0华氏度 1摄氏度
    extern volatile unsigned char timmer_1_name[100];             //定时器1名称
    extern volatile unsigned char timmer_2_name[100];             //定时器2名称
    extern volatile unsigned char global_grill_state;             //烤炉状态

    extern unsigned int menu_inform[11][4]; //菜单模式
    
    global_set_temp = AWS_Interface.Get_RTDSettings_Handle();
    
    //设定接收变量初值 未从AWS接收到数据的变量维持不变
    global_power_switch_command = global_power_switch;
    global_heat_switch_command = global_heat_switch;
    global_set_temp_command = global_set_temp;
    global_rtd1_set_temp_command = global_rtd1_set_temp;
    global_rtd2_set_temp_command = global_rtd2_set_temp;
    global_timmer_0_set_command = global_timmer_0_set;
    global_timmer_1_set_command = global_timmer_1_set;
    global_timmer_2_set_command = global_timmer_2_set;
    global_temp_unit_command = global_temp_unit;
    int ret = 0;
    for (int i = 0; i < 50; i++) //清空dest 字符串
    {
        dest[i] = '\0';
    }
    // ESP_LOGI(TAG, "received command is %.*s", received_command_length, received_command);
    for (int i = 0; i < 200; i++)
    {
        received_command[i] = received_command[i + 1];
    }
    //读取power on
    ret = strncmp((char *)received_command, "poweron", 7); //int strncmp(const char *str1, const char *str2, size_t n) 如果返回值等于 0，则表示 str1 等于 str2。
    if (ret == 0)
    {
        global_power_switch_command = 1;                      //电源开
        ESP_LOGI(TAG, "recognized the command \'power on\'"); //识别到指令poweron
        for (int i = 0; i < 200; i++)
        {
            received_command[i] = received_command[i + 8];
        }
        //读取command id
        int i = 0;
        for (int i = 0; i < 50; i++) //清空command id 字符串
        {

            command_id[i] = '\0';
        }
        command_id[0] = '1';
        while (strncmp(((char *)received_command + i), "\"", 1)) //统计空格前的数量
        {
            i++;
        }
        for (int j = 0; j < i; j++)
        {
            command_id[j] = received_command[j];
        }
        
        // memcpy(command_id, received_command, i);       //复制command id
        ESP_LOGI(TAG, "command id is %s", command_id); //获取到的command id
    }
    //读取power off
    ret = strncmp((char *)received_command, "poweroff", 8); //int strncmp(const char *str1, const char *str2, size_t n) 如果返回值等于 0，则表示 str1 等于 str2。
    if (ret == 0)
    {
        global_power_switch_command = 0;                  //电源关
        wireless_state.aws_received_command = get_inform; //从AWS获得指令
        ESP_LOGI(TAG, "recognized the command \'power off\'");
        for (int i = 0; i < 200; i++)
        {
            received_command[i] = received_command[i + 9];
        }
        //读取command id
        int i = 0;
        for (int i = 0; i < 50; i++) //清空command id 字符串
        {

            command_id[i] = '\0';
        }
        command_id[0] = '1';
        while (strncmp(((char *)received_command + i), "\"", 1)) //统计空格前的数量
        {
            i++;
        }
        for (int j = 0; j < i; j++)
        {
            command_id[j] = received_command[j];
        }
        // memcpy(command_id, received_command, i);       //复制command id
        ESP_LOGI(TAG, "command id is %s", command_id); //获取到的command id
    }
    //读取setemp
    ret = strncmp((char *)received_command, "setemp", 6); //int strncmp(const char *str1, const char *str2, size_t n) 如果返回值等于 0，则表示 str1 等于 str2。
    if (ret == 0)
    {
        ESP_LOGI(TAG, "recognized the command \'setemp\'");
        for (int i = 0; i < 200; i++)
        {
            received_command[i] = received_command[i + 7];
        }
        //设置rtd0
        ret = strncmp((char *)received_command, "rtd0", 4);
        if (ret == 0)
        {
            ESP_LOGI(TAG, "recognized the command setemp rtd0");
            for (int i = 0; i < 200; i++)
            {
                received_command[i] = received_command[i + 5];
                if(i < 3) dest[i] = received_command[i];
            }
            // memcpy(dest, received_command, 3); //复制前3位
            ESP_LOGI(TAG, "rtd0 value is %.*s", 3, dest);
            global_set_temp_command = atoi(dest);             //string 转 int
            wireless_state.aws_received_command = get_inform; //从AWS获得指令
            ESP_LOGI(TAG, "RTD 0 received value is %d", global_set_temp_command);
            for (int i = 0; i < 200; i++)
            {
                received_command[i] = received_command[i + 3];
            }

            if(AWS_Interface.Set_RTDSettings_Handle != NULL) AWS_Interface.Set_RTDSettings_Handle(global_set_temp_command);
        }
        //设置rtd1
        ret = strncmp((char *)received_command, "rtd1", 4);
        if (ret == 0)
        {
            ESP_LOGI(TAG, "recognized the command setemp rtd1");
            for (int i = 0; i < 200; i++)
            {
                received_command[i] = received_command[i + 5];
            }
            int i = 0;
            while (strncmp(((char *)received_command + i), " ", 1)) //统计空格前的数量
            {
                i++;
            }
            for (int j = 0; j < i; j++)
            {
                dest[j] = received_command[j];
            }
            
            // ESP_LOGI(TAG, "number length is %d", i);
            // memcpy(dest, received_command, i); //复制前i位
            ESP_LOGI(TAG, "rtd1 value is %.*s", i, dest);
            global_rtd1_set_temp_command = atoi(dest);        //string 转 int
            wireless_state.aws_received_command = get_inform; //从AWS获得指令
            ESP_LOGI(TAG, "RTD 1 received value is %d", global_rtd1_set_temp_command);
            for (int j = 0; j < 200; j++)
            {
                received_command[j] = received_command[j + i];
            }

            if(AWS_Interface.Set_ProbeASettings_Handle != NULL) AWS_Interface.Set_ProbeASettings_Handle(global_rtd1_set_temp_command);
        }
        // //设置rtd2
        ret = strncmp((char *)received_command, "rtd2", 4);
        if (ret == 0)
        {
            ESP_LOGI(TAG, "recognized the command setemp rtd2");
            for (int i = 0; i < 200; i++)
            {
                received_command[i] = received_command[i + 5];
            }
            int i = 0;
            while (strncmp(((char *)received_command + i), " ", 1)) //统计空格前的数量
            {
                i++;
            }
            for (int j = 0; j < i; j++)
            {
                dest[j] = received_command[j];
            }
            // memcpy(dest, received_command, i); //复制前i位
            // ESP_LOGI(TAG, "number length is %d", i);
            ESP_LOGI(TAG, "rtd2 value is %.*s", i, dest);
            global_rtd2_set_temp_command = atoi(dest);        //string 转 int
            wireless_state.aws_received_command = get_inform; //从AWS获得指令
            ESP_LOGI(TAG, "RTD 1 received value is %d", global_rtd2_set_temp_command);
            for (int j = 0; j < 200; j++)
            {
                received_command[j] = received_command[j + i];
            }

            if(AWS_Interface.Set_ProbeBSettings_Handle != NULL) AWS_Interface.Set_ProbeBSettings_Handle(global_rtd2_set_temp_command);
        }
        // params->payload += 1;
        //读取command id
        int i = 0;
        for (int i = 0; i < 50; i++) //清空command id 字符串
        {

            command_id[i] = '\0';
        }
        command_id[0] = '1';
        while (strncmp(((char *)received_command + i), "\"", 1)) //统计空格前的数量
        {
            i++;
        }
        for (int j = 0; j < i; j++)
        {
            command_id[j] = received_command[j];
        }
        // memcpy(command_id, received_command, i);       //复制command id
        ESP_LOGI(TAG, "command id is %s", command_id); //获取到的command id
    }
    //读取setmr
    ret = strncmp((char *)received_command, "setmr", 5); //int strncmp(const char *str1, const char *str2, size_t n) 如果返回值等于 0，则表示 str1 等于 str2。
    if (ret == 0)
    {
        //设置tmr1
        ESP_LOGI(TAG, "recognized the command \'setmr\'");
        for (int i = 0; i < 200; i++)
        {
            received_command[i] = received_command[i + 6];
        }
        ret = strncmp((char *)received_command, "tmr1", 4);
        if (ret == 0)
        {
            ESP_LOGI(TAG, "recognized the command setmr tmr1 ");
            for (int i = 0; i < 200; i++)
            {
                received_command[i] = received_command[i + 5];
            }
            int i = 0;
            while (strncmp(((char *)received_command + i), " ", 1)) //统计空格前的数量
            {
                i++; //获取数字长度
            }
            for (int j = 0; j < i; j++)
            {
                dest[j] = received_command[j];
            }
            
            ESP_LOGI(TAG, "number length is %d", i);                                   //打印数字长度
            // memcpy(dest, received_command, i);                                         //复制数字位
            ESP_LOGI(TAG, "tmr1 value is %.*s", i, dest);                              //打印数字位
            global_timmer_1_set_command = atoi(dest);                                  //string 转 int
            wireless_state.aws_received_command = get_inform;                          //从AWS获得指令
            ESP_LOGI(TAG, "tmr 1 received value is %ld", global_timmer_1_set_command); //打印获取到的数字
            for (int j = 0; j < 200; j++)
            {
                received_command[j] = received_command[j + i + 1];
            }

            if(AWS_Interface.Set_Timer1_Handle != NULL) AWS_Interface.Set_Timer1_Handle(global_timmer_1_set_command);
        }
        //设置tmr2
        ret = strncmp((char *)received_command, "tmr2", 4);
        if (ret == 0)
        {
            ESP_LOGI(TAG, "recognized the command setmr tmr2 ");
            for (int i = 0; i < 200; i++)
            {
                received_command[i] = received_command[i + 5];
            }
            int i = 0;
            while (strncmp(((char *)received_command + i), " ", 1)) //统计空格前的数量
            {
                i++; //获取数字长度
            }
            for (int j = 0; j < i; j++)
            {
                dest[j] = received_command[j];
            }
            
            ESP_LOGI(TAG, "number length is %d", i);                                   //打印数字长度
            // memcpy(dest, received_command, i);                                         //复制数字位
            ESP_LOGI(TAG, "tmr2 value is %.*s", i, dest);                              //打印数字位
            global_timmer_2_set_command = atoi(dest);                                  //string 转 int
            wireless_state.aws_received_command = get_inform;                          //从AWS获得指令
            ESP_LOGI(TAG, "tmr 2 received value is %ld", global_timmer_2_set_command); //打印获取到的数字
            for (int j = 0; j < 200; j++)
            {
                received_command[j] = received_command[j + i + 1];
            }

            if(AWS_Interface.Set_Timer2_Handle != NULL) AWS_Interface.Set_Timer2_Handle(global_timmer_2_set_command);
        }
        //读取command id
        int i = 0;
        for (int i = 0; i < 50; i++) //清空command id 字符串
        {

            command_id[i] = '\0';
        }
        command_id[0] = '1';
        while (strncmp(((char *)received_command + i), "\"", 1)) //统计空格前的数量
        {
            i++;
        }
        for (int j = 0; j < i; j++)
        {
            command_id[j] = received_command[j];
        }
        // memcpy(command_id, received_command, i);       //复制command id
        ESP_LOGI(TAG, "command id is %s", command_id); //获取到的command id
    }
    //读取setunit
    ret = strncmp((char *)received_command, "setunit", 7); //int strncmp(const char *str1, const char *str2, size_t n) 如果返回值等于 0，则表示 str1 等于 str2。
    if (ret == 0)
    {
        for (int i = 0; i < 200; i++)
        {
            received_command[i] = received_command[i + 8];
        }
        ESP_LOGI(TAG, "%s", received_command); //获取到的command id
        ret = strncmp((char *)received_command, "3", 1);
        if (ret == 0)
        {
            global_temp_unit_command = Unit_C;
            wireless_state.aws_received_command = get_inform; //从AWS获得指令
            ESP_LOGI(TAG, "set temp unit C");                 //设置为摄氏度
        }
        ret = strncmp((char *)received_command, "2", 1);
        if (ret == 0)
        {
            global_temp_unit_command = Unit_F;
            wireless_state.aws_received_command = get_inform; //从AWS获得指令
            ESP_LOGI(TAG, "set temp unit F");                 //设置为华氏度
        }
        for (int i = 0; i < 200; i++)
        {
            received_command[i] = received_command[i + 2];
        }
        //读取command id
        int i = 0;
        for (int i = 0; i < 50; i++) //清空command id 字符串
        {

            command_id[i] = '\0';
        }
        command_id[0] = '1';
        while (strncmp(((char *)received_command + i), "\"", 1)) //统计空格前的数量
        {
            i++;
        }
        for (int j = 0; j < i; j++)
        {
            command_id[j] = received_command[j];
        }
        // memcpy(command_id, received_command, i);       //复制command id
        ESP_LOGI(TAG, "command id is %s", command_id); //获取到的command id
        
        if(AWS_Interface.SetUnit_Handle != NULL) AWS_Interface.SetUnit_Handle(global_temp_unit_command);
    }
    //读取ignite
    ret = strncmp((char *)received_command, "ignite", 6); //int strncmp(const char *str1, const char *str2, size_t n) 如果返回值等于 0，则表示 str1 等于 str2。
    if (ret == 0)
    {
        global_heat_switch_command = 1;
        global_grill_state = 3;                           //取消烟熏或保温模式
        wireless_state.aws_received_command = get_inform; //从AWS获得指令
        ESP_LOGI(TAG, "heat switch on");                  //加热开关开
        for (int i = 0; i < 200; i++)
        {
            received_command[i] = received_command[i + 7];
        }
        //读取command id
        int i = 0;
        for (int i = 0; i < 50; i++) //清空command id 字符串
        {

            command_id[i] = '\0';
        }
        command_id[0] = '1';
        while (strncmp(((char *)received_command + i), "\"", 1)) //统计空格前的数量
        {
            i++;
        }
        for (int j = 0; j < i; j++)
        {
            command_id[j] = received_command[j];
        }
        // memcpy(command_id, received_command, i);       //复制command id
        ESP_LOGI(TAG, "command id is %s", command_id); //获取到的command id

        if(AWS_Interface.Ignite_Handle != NULL) AWS_Interface.Ignite_Handle();
        if((UI_System.Grill_Smoke == State_Grill_Smoke) || (UI_System.Grill_state == State_Grill_KeepWarm)) UI_System.Grill_state = State_Grill_Cook;
    }
    //读取shut off
    ret = strncmp((char *)received_command, "shutoff", 7); //int strncmp(const char *str1, const char *str2, size_t n) 如果返回值等于 0，则表示 str1 等于 str2。
    if (ret == 0)
    {
        global_heat_switch_command = 0;                   //加热开关关
        wireless_state.aws_received_command = get_inform; //从AWS获得指令
        ESP_LOGI(TAG, "heat switch off");
        for (int i = 0; i < 200; i++)
        {
            received_command[i] = received_command[i + 8];
        }
        //读取command id
        int i = 0;
        for (int i = 0; i < 50; i++) //清空command id 字符串
        {

            command_id[i] = '\0';
        }
        command_id[0] = '1';
        while (strncmp(((char *)received_command + i), "\"", 1)) //统计空格前的数量
        {
            i++;
        }
        for (int j = 0; j < i; j++)
        {
            command_id[j] = received_command[j];
        }
        // memcpy(command_id, received_command, i);       //复制command id
        ESP_LOGI(TAG, "command id is %s", command_id); //获取到的command id

        if(AWS_Interface.Shuttdown_Handle != NULL) AWS_Interface.Shuttdown_Handle();
    }
    //读取cancel
    ret = strncmp((char *)received_command, "cancel", 6); //int strncmp(const char *str1, const char *str2, size_t n) 如果返回值等于 0，则表示 str1 等于 str2。
    if (ret == 0)
    {
        global_cancel_shutdown_command = 1;                 //取消熄火
        wireless_state.aws_received_command = get_inform;   //从AWS获得指令
        ESP_LOGI(TAG, "recognized the command \'cancel\'"); //识别到指令poweron
        for (int i = 0; i < 200; i++)
        {
            received_command[i] = received_command[i + 7];
        }
        //读取command id
        int i = 0;
        for (int i = 0; i < 50; i++) //清空command id 字符串
        {

            command_id[i] = '\0';
        }
        command_id[0] = '1';
        while (strncmp(((char *)received_command + i), "\"", 1)) //统计空格前的数量
        {
            i++;
        }
        for (int j = 0; j < i; j++)
        {
            command_id[j] = received_command[j];
        }
        // memcpy(command_id, received_command, i);       //复制command id
        ESP_LOGI(TAG, "command id is %s", command_id); //获取到的command id

        if(AWS_Interface.Shuttdown_Cancel_Handle != NULL) AWS_Interface.Shuttdown_Cancel_Handle();
    }
    //读取smoke state==5时 state=8
    ret = strncmp((char *)received_command, "smoke", 5); //int strncmp(const char *str1, const char *str2, size_t n) 如果返回值等于 0，则表示 str1 等于 str2。
    if (ret == 0)
    {
        wireless_state.aws_received_command = get_inform; //从AWS获得指令
        ESP_LOGI(TAG, "recognized the command smoke");    //识别到指令poweron
        if (global_grill_state == 5)
        {
            global_grill_state = 8;
        }
        for (int i = 0; i < 200; i++)
        {
            received_command[i] = received_command[i + 6];
        }
        //读取command id
        int i = 0;
        for (int i = 0; i < 50; i++) //清空command id 字符串
        {
            command_id[i] = '\0';
        }
        command_id[0] = '1';
        while (strncmp(((char *)received_command + i), "\"", 1)) //统计空格前的数量
        {
            i++;
        }
        for (int j = 0; j < i; j++)
        {
            command_id[j] = received_command[j];
        }
        // memcpy(command_id, received_command, i);       //复制command id
        ESP_LOGI(TAG, "command id is %s", command_id); //获取到的command id

        // if((UI_System.Grill_state == State_Grill_Ignite) || (UI_System.Grill_state == State_Grill_Cook) || (UI_System.Grill_state == State_Grill_KeepWarm))
        // {
        //     if(AWS_Interface.Set_RTDSettings_Handle != NULL) AWS_Interface.Set_RTDSettings_Handle(165);
        //     UI_System.Grill_Smoke = State_Grill_Smoke;
        // }
        // else if(UI_System.Grill_Smoke == State_Grill_Smoke)
        // {
        //     UI_System.Grill_state = State_Grill_Cook;
        // }
    }
    //读取keepwarm state==5时 state=9
    ret = strncmp((char *)received_command, "keepwarm", 8); //int strncmp(const char *str1, const char *str2, size_t n) 如果返回值等于 0，则表示 str1 等于 str2。
    if (ret == 0)
    {
        wireless_state.aws_received_command = get_inform; //从AWS获得指令
        ESP_LOGI(TAG, "recognized the command keepwarm"); //识别到指令poweron
        if (global_grill_state == 5)
        {
            global_grill_state = 9;
        }
        for (int i = 0; i < 200; i++)
        {
            received_command[i] = received_command[i + 9];
        }
        //读取command id
        int i = 0;
        for (int i = 0; i < 50; i++) //清空command id 字符串
        {
            command_id[i] = '\0';
        }
        command_id[0] = '1';
        while (strncmp(((char *)received_command + i), "\"", 1)) //统计空格前的数量
        {
            i++;
        }
        for (int j = 0; j < i; j++)
        {
            command_id[j] = received_command[j];
        }
        // memcpy(command_id, received_command, i);       //复制command id
        ESP_LOGI(TAG, "command id is %s", command_id); //获取到的command id

        if((UI_System.Grill_state == State_Grill_Ignite) || (UI_System.Grill_state == State_Grill_Cook) || (UI_System.Grill_Smoke = State_Grill_Smoke))
        {
            if(AWS_Interface.Set_RTDSettings_Handle != NULL) AWS_Interface.Set_RTDSettings_Handle(165);
            UI_System.Grill_state = State_Grill_KeepWarm;
        }
        else if(UI_System.Grill_state == State_Grill_KeepWarm)
        {
            UI_System.Grill_state = State_Grill_Cook;
        }
    }
    //读取 status 1 (仅供开发板调试用)
    ret = strncmp((char *)received_command, "status", 6); //int strncmp(const char *str1, const char *str2, size_t n) 如果返回值等于 0，则表示 str1 等于 str2。
    if (ret == 0)
    {
        wireless_state.aws_received_command = get_inform; //从AWS获得指令
        for (int i = 0; i < 200; i++)
        {
            received_command[i] = received_command[i + 7];
        }
        for (int i = 0; i < 50; i++) //清空dest字符串
        {
            dest[i] = '\0';
        }
        for (int j = 0; j < 1; j++)
        {
            dest[j] = received_command[j];
        }
        // memcpy(dest, received_command, 1); //复制数字位
        global_grill_state = atoi(dest);   //string 转 int
        ESP_LOGI(TAG, "receive status is %d", global_grill_state);
        //读取command id
        // int i = 0;
        // for (int i = 0; i < 50; i++) //清空command id 字符串
        // {

        //     command_id[i] = '\0';
        // }
        // command_id[0] = '1';
        // while (strncmp(((char *)received_command + i), "\"", 1)) //统计空格前的数量
        // {
        //     i++;
        // }
        // memcpy(command_id, received_command, i);       //复制command id
        // ESP_LOGI(TAG, "command id is %s", command_id); //获取到的command id
    }
    //读取菜单模式
    ret = strncmp((char *)received_command, "setall", 6); //int strncmp(const char *str1, const char *str2, size_t n) 如果返回值等于 0，则表示 str1 等于 str2。
    if (ret == 0)
    {
        ESP_LOGI(TAG, "command \'setall\'");
        for (int i = 0; i < 200; i++)
        {
            received_command[i] = received_command[i + 7];
        }

        //清空数组
        for (int i = 0; i < 11; i++)
        {
            for (int j = 0; j < 4; j++)
            {
                menu_inform[i][j] = 0;
            }
        }
        for (int i = 0; i < 50; i++) //清空dest字符串
        {
            dest[i] = '\0';
        }
        // received_command_length -= 7;
        // ESP_LOGI(TAG, "received command is %.*s", received_command_length, received_command);
        for (int i = 0; i < 11; i++)
        {
            for (int j = 0; j < 4; j++)
            {
                while (strncmp(((char *)received_command + len), " ", 1) != 0) //统计空格前的数量
                {
                    if((received_command[len] == 34) || (received_command[len] == 0)) break; //校验结束符"
                    len++;
                    if (len > 10)
                    {
                        ESP_LOGI(TAG, "receive error break 1");
                        continue;
                    }
                }
                if(received_command[len] == 0) break; //校验结束符"
                for (int j = 0; j < len; j++)
                {
                    dest[j] = received_command[j];
                }
                // memcpy(dest, received_command, len); //复制数字位
                menu_inform[i][j] = atoi(dest);      //string 转 int
                if(received_command[len] == 34)
                {
                    CooK_MenuItem_Length = i;
                    menu_inform[i][j] = 0;
                }
                ESP_LOGI(TAG, "menu_inform[%d][%d] = %d ", i, j, menu_inform[i][j]);

                for (int k = 0; k < 200; k++)
                {
                    received_command[k] = received_command[k + len + 1];
                }
                if (len < 10) len = 0;
                for (int k = 0; k < 50; k++) //清空dest字符串
                {
                    dest[k] = '\0';
                }
                //错误指令
                if (len > 10)
                {
                    ESP_LOGI(TAG, "receive error break 2");
                    continue;
                }
            }
            if (len > 10)
            {
                for (int i = 0; i < 11; i++)
                {
                    for (int j = 0; j < 4; j++)
                    {
                        menu_inform[i][j] = 0;
                    }
                }
                ESP_LOGI(TAG, "receive error break 3");
                break;
            }
        }
        //读取command id
        int i = 0;
        for (int i = 0; i < 50; i++) //清空command id 字符串
        {
            command_id[i] = '\0';
        }
        command_id[0] = '1';
        while (strncmp(((char *)received_command + i), "\"", 1)) //统计空格前的数量
        {
            i++;
        }
        for (int j = 0; j < i; j++)
        {
            command_id[j] = received_command[j];
        }
        // memcpy(command_id, received_command, i);       //复制command id
        ESP_LOGI(TAG, "command id is %s", command_id); //获取到的command id
            
        if(AWS_Interface.SetMenu_Handle != NULL) AWS_Interface.SetMenu_Handle();
        
    }
    //清空缓存
    // received_command_length = 0;
    for (int i = 0; i < 200; i++)
    {
        received_command[i] = '\0';
    }
}

//aws 接收数据
void aws_receive(void *param)
{
    unsigned char len = 0;
    char dest[100];                             //临时变量
    extern volatile unsigned int ID_first_four; //MAC初4位做识别
    extern volatile unsigned int ID_mid_four;   //MAC中间4位做识别
    extern volatile unsigned int ID_last_four;  //MAC最后4位做识别

    extern volatile unsigned char global_power_switch_command; //电源开关
    extern volatile unsigned char global_heat_switch_command;  //加热开关
    extern volatile unsigned int global_set_temp_command;      //设置温度
    extern volatile unsigned int global_rtd1_set_temp_command; //RTD1报警温度
    extern volatile unsigned int global_rtd2_set_temp_command; //RTD2报警温度
    extern volatile unsigned long global_timmer_0_set_command; //定时器0设定值 用于设置菜单的运行时间
    extern volatile unsigned long global_timmer_1_set_command; //定时器1设定值
    extern volatile unsigned long global_timmer_2_set_command; //定时器2设定值
    extern volatile unsigned char global_temp_unit_command;    //温度单位 0华氏度 1摄氏度
    extern volatile unsigned int firmware_number;              //固件版本号

    extern volatile unsigned char global_power_switch;            //电源开关
    extern volatile unsigned char global_heat_switch;             //加热开关
    extern volatile unsigned char global_cancel_shutdown_command; //取消熄火
    extern volatile unsigned int global_set_temp;                 //设置温度
    extern volatile unsigned int global_rtd1_set_temp;            //RTD1报警温度
    extern volatile unsigned int global_rtd2_set_temp;            //RTD2报警温度
    extern volatile unsigned long global_timmer_0_set;            //定时器0设定值 用于设置菜单的运行时间
    extern volatile unsigned long global_timmer_1_set;            //定时器1设定值
    extern volatile unsigned long global_timmer_2_set;            //定时器2设定值
    extern volatile unsigned char global_temp_unit;               //温度单位 0华氏度 1摄氏度
    extern volatile unsigned char timmer_1_name[100];             //定时器1名称
    extern volatile unsigned char timmer_2_name[100];             //定时器2名称
    extern volatile unsigned char global_grill_state;             //烤炉状态

    extern unsigned int menu_inform[11][4]; //菜单模式

    while (1)
    {
        if (received_command_length > 0)
        {
            global_set_temp = AWS_Interface.Get_RTDSettings_Handle();
            
            //设定接收变量初值 未从AWS接收到数据的变量维持不变
            global_power_switch_command = global_power_switch;
            global_heat_switch_command = global_heat_switch;
            global_set_temp_command = global_set_temp;
            global_rtd1_set_temp_command = global_rtd1_set_temp;
            global_rtd2_set_temp_command = global_rtd2_set_temp;
            global_timmer_0_set_command = global_timmer_0_set;
            global_timmer_1_set_command = global_timmer_1_set;
            global_timmer_2_set_command = global_timmer_2_set;
            global_temp_unit_command = global_temp_unit;
            int ret = 0;
            for (int i = 0; i < 50; i++) //清空dest 字符串
            {
                dest[i] = '\0';
            }
            // ESP_LOGI(TAG, "received command is %.*s", received_command_length, received_command);
            for (int i = 0; i < 200; i++)
            {
                received_command[i] = received_command[i + 1];
            }
            //读取power on
            ret = strncmp((char *)received_command, "poweron", 7); //int strncmp(const char *str1, const char *str2, size_t n) 如果返回值等于 0，则表示 str1 等于 str2。
            if (ret == 0)
            {
                global_power_switch_command = 1;                      //电源开
                ESP_LOGI(TAG, "recognized the command \'power on\'"); //识别到指令poweron
                for (int i = 0; i < 200; i++)
                {
                    received_command[i] = received_command[i + 8];
                }
                //读取command id
                int i = 0;
                for (int i = 0; i < 50; i++) //清空command id 字符串
                {

                    command_id[i] = '\0';
                }
                command_id[0] = '1';
                while (strncmp(((char *)received_command + i), "\"", 1)) //统计空格前的数量
                {
                    i++;
                }
                for (int j = 0; j < i; j++)
                {
                    command_id[j] = received_command[j];
                }
                // memcpy(command_id, received_command, i);       //复制command id
                ESP_LOGI(TAG, "command id is %s", command_id); //获取到的command id
            }
            //读取power off
            ret = strncmp((char *)received_command, "poweroff", 8); //int strncmp(const char *str1, const char *str2, size_t n) 如果返回值等于 0，则表示 str1 等于 str2。
            if (ret == 0)
            {
                global_power_switch_command = 0;                  //电源关
                wireless_state.aws_received_command = get_inform; //从AWS获得指令
                ESP_LOGI(TAG, "recognized the command \'power off\'");
                for (int i = 0; i < 200; i++)
                {
                    received_command[i] = received_command[i + 9];
                }
                //读取command id
                int i = 0;
                for (int i = 0; i < 50; i++) //清空command id 字符串
                {

                    command_id[i] = '\0';
                }
                command_id[0] = '1';
                while (strncmp(((char *)received_command + i), "\"", 1)) //统计空格前的数量
                {
                    i++;
                }
                for (int j = 0; j < i; j++)
                {
                    command_id[j] = received_command[j];
                }
                // memcpy(command_id, received_command, i);       //复制command id
                ESP_LOGI(TAG, "command id is %s", command_id); //获取到的command id
            }
            //读取setemp
            ret = strncmp((char *)received_command, "setemp", 6); //int strncmp(const char *str1, const char *str2, size_t n) 如果返回值等于 0，则表示 str1 等于 str2。
            if (ret == 0)
            {
                ESP_LOGI(TAG, "recognized the command \'setemp\'");
                for (int i = 0; i < 200; i++)
                {
                    received_command[i] = received_command[i + 7];
                }
                //设置rtd0
                ret = strncmp((char *)received_command, "rtd0", 4);
                if (ret == 0)
                {
                    ESP_LOGI(TAG, "recognized the command setemp rtd0");
                    for (int i = 0; i < 200; i++)
                    {
                        received_command[i] = received_command[i + 5];
                    }
                    for (int j = 0; j < 3; j++)
                    {
                        dest[j] = received_command[j];
                    }
                    // memcpy(dest, received_command, 3); //复制前3位
                    ESP_LOGI(TAG, "rtd0 value is %.*s", 3, dest);
                    global_set_temp_command = atoi(dest);             //string 转 int
                    wireless_state.aws_received_command = get_inform; //从AWS获得指令
                    ESP_LOGI(TAG, "RTD 0 received value is %d", global_set_temp_command);
                    for (int i = 0; i < 200; i++)
                    {
                        received_command[i] = received_command[i + 3];
                    }

                    if(AWS_Interface.Set_RTDSettings_Handle != NULL) AWS_Interface.Set_RTDSettings_Handle(global_set_temp_command);
                }
                //设置rtd1
                ret = strncmp((char *)received_command, "rtd1", 4);
                if (ret == 0)
                {
                    ESP_LOGI(TAG, "recognized the command setemp rtd1");
                    for (int i = 0; i < 200; i++)
                    {
                        received_command[i] = received_command[i + 5];
                    }
                    int i = 0;
                    while (strncmp(((char *)received_command + i), " ", 1)) //统计空格前的数量
                    {
                        i++;
                    }
                    for (int j = 0; j < i; j++)
                    {
                        dest[j] = received_command[j];
                    }
                    // ESP_LOGI(TAG, "number length is %d", i);
                    // memcpy(dest, received_command, i); //复制前i位
                    ESP_LOGI(TAG, "rtd1 value is %.*s", i, dest);
                    global_rtd1_set_temp_command = atoi(dest);        //string 转 int
                    wireless_state.aws_received_command = get_inform; //从AWS获得指令
                    ESP_LOGI(TAG, "RTD 1 received value is %d", global_rtd1_set_temp_command);
                    for (int j = 0; j < 200; j++)
                    {
                        received_command[j] = received_command[j + i];
                    }

                    if(AWS_Interface.Set_ProbeASettings_Handle != NULL) AWS_Interface.Set_ProbeASettings_Handle(global_rtd1_set_temp_command);
                }
                // //设置rtd2
                ret = strncmp((char *)received_command, "rtd2", 4);
                if (ret == 0)
                {
                    ESP_LOGI(TAG, "recognized the command setemp rtd2");
                    for (int i = 0; i < 200; i++)
                    {
                        received_command[i] = received_command[i + 5];
                    }
                    int i = 0;
                    while (strncmp(((char *)received_command + i), " ", 1)) //统计空格前的数量
                    {
                        i++;
                    }
                    for (int j = 0; j < i; j++)
                    {
                        dest[j] = received_command[j];
                    }
                    // memcpy(dest, received_command, i); //复制前i位
                    // ESP_LOGI(TAG, "number length is %d", i);
                    ESP_LOGI(TAG, "rtd2 value is %.*s", i, dest);
                    global_rtd2_set_temp_command = atoi(dest);        //string 转 int
                    wireless_state.aws_received_command = get_inform; //从AWS获得指令
                    ESP_LOGI(TAG, "RTD 1 received value is %d", global_rtd2_set_temp_command);
                    for (int j = 0; j < 200; j++)
                    {
                        received_command[j] = received_command[j + i];
                    }

                    if(AWS_Interface.Set_ProbeBSettings_Handle != NULL) AWS_Interface.Set_ProbeBSettings_Handle(global_rtd2_set_temp_command);
                }
                // params->payload += 1;
                //读取command id
                int i = 0;
                for (int i = 0; i < 50; i++) //清空command id 字符串
                {

                    command_id[i] = '\0';
                }
                command_id[0] = '1';
                while (strncmp(((char *)received_command + i), "\"", 1)) //统计空格前的数量
                {
                    i++;
                }
                for (int j = 0; j < i; j++)
                {
                    command_id[j] = received_command[j];
                }
                // memcpy(command_id, received_command, i);       //复制command id
                ESP_LOGI(TAG, "command id is %s", command_id); //获取到的command id
            }
            //读取setmr
            ret = strncmp((char *)received_command, "setmr", 5); //int strncmp(const char *str1, const char *str2, size_t n) 如果返回值等于 0，则表示 str1 等于 str2。
            if (ret == 0)
            {
                //设置tmr1
                ESP_LOGI(TAG, "recognized the command \'setmr\'");
                for (int i = 0; i < 200; i++)
                {
                    received_command[i] = received_command[i + 6];
                }
                ret = strncmp((char *)received_command, "tmr1", 4);
                if (ret == 0)
                {
                    ESP_LOGI(TAG, "recognized the command setmr tmr1 ");
                    for (int i = 0; i < 200; i++)
                    {
                        received_command[i] = received_command[i + 5];
                    }
                    int i = 0;
                    while (strncmp(((char *)received_command + i), " ", 1)) //统计空格前的数量
                    {
                        i++; //获取数字长度
                    }
                    for (int j = 0; j < i; j++)
                    {
                        dest[j] = received_command[j];
                    }
                    ESP_LOGI(TAG, "number length is %d", i);                                   //打印数字长度
                    // memcpy(dest, received_command, i);                                         //复制数字位
                    ESP_LOGI(TAG, "tmr1 value is %.*s", i, dest);                              //打印数字位
                    global_timmer_1_set_command = atoi(dest);                                  //string 转 int
                    wireless_state.aws_received_command = get_inform;                          //从AWS获得指令
                    ESP_LOGI(TAG, "tmr 1 received value is %ld", global_timmer_1_set_command); //打印获取到的数字
                    for (int j = 0; j < 200; j++)
                    {
                        received_command[j] = received_command[j + i + 1];
                    }

                    if(AWS_Interface.Set_Timer1_Handle != NULL) AWS_Interface.Set_Timer1_Handle(global_timmer_1_set_command);
                }
                //设置tmr2
                ret = strncmp((char *)received_command, "tmr2", 4);
                if (ret == 0)
                {
                    ESP_LOGI(TAG, "recognized the command setmr tmr2 ");
                    for (int i = 0; i < 200; i++)
                    {
                        received_command[i] = received_command[i + 5];
                    }
                    int i = 0;
                    while (strncmp(((char *)received_command + i), " ", 1)) //统计空格前的数量
                    {
                        i++; //获取数字长度
                    }
                    ESP_LOGI(TAG, "number length is %d", i);                                   //打印数字长度
                    for (int j = 0; j < i; j++)
                    {
                        dest[j] = received_command[j];
                    }
                    // memcpy(dest, received_command, i);                                         //复制数字位
                    ESP_LOGI(TAG, "tmr2 value is %.*s", i, dest);                              //打印数字位
                    global_timmer_2_set_command = atoi(dest);                                  //string 转 int
                    wireless_state.aws_received_command = get_inform;                          //从AWS获得指令
                    ESP_LOGI(TAG, "tmr 2 received value is %ld", global_timmer_2_set_command); //打印获取到的数字
                    for (int j = 0; j < 200; j++)
                    {
                        received_command[j] = received_command[j + i + 1];
                    }

                    if(AWS_Interface.Set_Timer2_Handle != NULL) AWS_Interface.Set_Timer2_Handle(global_timmer_2_set_command);
                }
                //读取command id
                int i = 0;
                for (int i = 0; i < 50; i++) //清空command id 字符串
                {

                    command_id[i] = '\0';
                }
                command_id[0] = '1';
                while (strncmp(((char *)received_command + i), "\"", 1)) //统计空格前的数量
                {
                    i++;
                }
                for (int j = 0; j < i; j++)
                {
                    command_id[j] = received_command[j];
                }
                // memcpy(command_id, received_command, i);       //复制command id
                ESP_LOGI(TAG, "command id is %s", command_id); //获取到的command id
            }
            //读取setunit
            ret = strncmp((char *)received_command, "setunit", 7); //int strncmp(const char *str1, const char *str2, size_t n) 如果返回值等于 0，则表示 str1 等于 str2。
            if (ret == 0)
            {
                for (int i = 0; i < 200; i++)
                {
                    received_command[i] = received_command[i + 8];
                }
                ret = strncmp((char *)received_command, "1", 1);
                if (ret == 0)
                {
                    global_temp_unit_command = 1;
                    wireless_state.aws_received_command = get_inform; //从AWS获得指令
                    ESP_LOGI(TAG, "set temp unit C");                 //设置为摄氏度
                }
                ret = strncmp((char *)received_command, "0", 1);
                if (ret == 0)
                {
                    global_temp_unit_command = 0;
                    wireless_state.aws_received_command = get_inform; //从AWS获得指令
                    ESP_LOGI(TAG, "set temp unit F");                 //设置为华氏度
                }
                for (int i = 0; i < 200; i++)
                {
                    received_command[i] = received_command[i + 2];
                }
                //读取command id
                int i = 0;
                for (int i = 0; i < 50; i++) //清空command id 字符串
                {

                    command_id[i] = '\0';
                }
                command_id[0] = '1';
                while (strncmp(((char *)received_command + i), "\"", 1)) //统计空格前的数量
                {
                    i++;
                }
                for (int j = 0; j < i; j++)
                {
                    command_id[j] = received_command[j];
                }
                // memcpy(command_id, received_command, i);       //复制command id
                ESP_LOGI(TAG, "command id is %s", command_id); //获取到的command id
                
                if(AWS_Interface.SetUnit_Handle != NULL) AWS_Interface.SetUnit_Handle(global_temp_unit_command);
            }
            //读取ignite
            ret = strncmp((char *)received_command, "ignite", 6); //int strncmp(const char *str1, const char *str2, size_t n) 如果返回值等于 0，则表示 str1 等于 str2。
            if (ret == 0)
            {
                global_heat_switch_command = 1;
                global_grill_state = 3;                           //取消烟熏或保温模式
                wireless_state.aws_received_command = get_inform; //从AWS获得指令
                ESP_LOGI(TAG, "heat switch on");                  //加热开关开
                for (int i = 0; i < 200; i++)
                {
                    received_command[i] = received_command[i + 7];
                }
                //读取command id
                int i = 0;
                for (int i = 0; i < 50; i++) //清空command id 字符串
                {

                    command_id[i] = '\0';
                }
                command_id[0] = '1';
                while (strncmp(((char *)received_command + i), "\"", 1)) //统计空格前的数量
                {
                    i++;
                }
                for (int j = 0; j < i; j++)
                {
                    command_id[j] = received_command[j];
                }
                // memcpy(command_id, received_command, i);       //复制command id
                ESP_LOGI(TAG, "command id is %s", command_id); //获取到的command id

                if(AWS_Interface.Ignite_Handle != NULL) AWS_Interface.Ignite_Handle();
                if((UI_System.Grill_Smoke == State_Grill_Smoke) || (UI_System.Grill_state == State_Grill_KeepWarm)) UI_System.Grill_state = State_Grill_Cook;
            }
            //读取shut off
            ret = strncmp((char *)received_command, "shutoff", 7); //int strncmp(const char *str1, const char *str2, size_t n) 如果返回值等于 0，则表示 str1 等于 str2。
            if (ret == 0)
            {
                global_heat_switch_command = 0;                   //加热开关关
                wireless_state.aws_received_command = get_inform; //从AWS获得指令
                ESP_LOGI(TAG, "heat switch off");
                for (int i = 0; i < 200; i++)
                {
                    received_command[i] = received_command[i + 8];
                }
                //读取command id
                int i = 0;
                for (int i = 0; i < 50; i++) //清空command id 字符串
                {

                    command_id[i] = '\0';
                }
                command_id[0] = '1';
                while (strncmp(((char *)received_command + i), "\"", 1)) //统计空格前的数量
                {
                    i++;
                }
                for (int j = 0; j < i; j++)
                {
                    command_id[j] = received_command[j];
                }
                // memcpy(command_id, received_command, i);       //复制command id
                ESP_LOGI(TAG, "command id is %s", command_id); //获取到的command id

                if(AWS_Interface.Shuttdown_Handle != NULL) AWS_Interface.Shuttdown_Handle();
            }
            //读取cancel
            ret = strncmp((char *)received_command, "cancel", 6); //int strncmp(const char *str1, const char *str2, size_t n) 如果返回值等于 0，则表示 str1 等于 str2。
            if (ret == 0)
            {
                global_cancel_shutdown_command = 1;                 //取消熄火
                wireless_state.aws_received_command = get_inform;   //从AWS获得指令
                ESP_LOGI(TAG, "recognized the command \'cancel\'"); //识别到指令poweron
                for (int i = 0; i < 200; i++)
                {
                    received_command[i] = received_command[i + 7];
                }
                //读取command id
                int i = 0;
                for (int i = 0; i < 50; i++) //清空command id 字符串
                {

                    command_id[i] = '\0';
                }
                command_id[0] = '1';
                while (strncmp(((char *)received_command + i), "\"", 1)) //统计空格前的数量
                {
                    i++;
                }
                for (int j = 0; j < i; j++)
                {
                    command_id[j] = received_command[j];
                }
                // memcpy(command_id, received_command, i);       //复制command id
                ESP_LOGI(TAG, "command id is %s", command_id); //获取到的command id

                if(AWS_Interface.Shuttdown_Cancel_Handle != NULL) AWS_Interface.Shuttdown_Cancel_Handle();
            }
            //读取smoke state==5时 state=8
            ret = strncmp((char *)received_command, "smoke", 5); //int strncmp(const char *str1, const char *str2, size_t n) 如果返回值等于 0，则表示 str1 等于 str2。
            if (ret == 0)
            {
                wireless_state.aws_received_command = get_inform; //从AWS获得指令
                ESP_LOGI(TAG, "recognized the command smoke");    //识别到指令poweron
                if (global_grill_state == 5)
                {
                    global_grill_state = 8;
                }
                for (int i = 0; i < 200; i++)
                {
                    received_command[i] = received_command[i + 6];
                }
                //读取command id
                int i = 0;
                for (int i = 0; i < 50; i++) //清空command id 字符串
                {
                    command_id[i] = '\0';
                }
                command_id[0] = '1';
                while (strncmp(((char *)received_command + i), "\"", 1)) //统计空格前的数量
                {
                    i++;
                }
                for (int j = 0; j < i; j++)
                {
                    command_id[j] = received_command[j];
                }
                // memcpy(command_id, received_command, i);       //复制command id
                ESP_LOGI(TAG, "command id is %s", command_id); //获取到的command id

                // if((UI_System.Grill_state == State_Grill_Ignite) || (UI_System.Grill_state == State_Grill_Cook) || (UI_System.Grill_state == State_Grill_KeepWarm))
                // {
                //     if(AWS_Interface.Set_RTDSettings_Handle != NULL) AWS_Interface.Set_RTDSettings_Handle(165);
                //     UI_System.Grill_Smoke = State_Grill_Smoke;
                // }
                // else if(UI_System.Grill_Smoke == State_Grill_Smoke)
                // {
                //     UI_System.Grill_state = State_Grill_Cook;
                // }
            }
            //读取keepwarm state==5时 state=9
            ret = strncmp((char *)received_command, "keepwarm", 8); //int strncmp(const char *str1, const char *str2, size_t n) 如果返回值等于 0，则表示 str1 等于 str2。
            if (ret == 0)
            {
                wireless_state.aws_received_command = get_inform; //从AWS获得指令
                ESP_LOGI(TAG, "recognized the command keepwarm"); //识别到指令poweron
                if (global_grill_state == 5)
                {
                    global_grill_state = 9;
                }
                for (int i = 0; i < 200; i++)
                {
                    received_command[i] = received_command[i + 9];
                }
                //读取command id
                int i = 0;
                for (int i = 0; i < 50; i++) //清空command id 字符串
                {
                    command_id[i] = '\0';
                }
                command_id[0] = '1';
                while (strncmp(((char *)received_command + i), "\"", 1)) //统计空格前的数量
                {
                    i++;
                }
                for (int j = 0; j < i; j++)
                {
                    command_id[j] = received_command[j];
                }
                // memcpy(command_id, received_command, i);       //复制command id
                ESP_LOGI(TAG, "command id is %s", command_id); //获取到的command id

                // if((UI_System.Grill_state == State_Grill_Ignite) || (UI_System.Grill_state == State_Grill_Cook) || (UI_System.Grill_Smoke = State_Grill_Smoke))
                // {
                //     if(AWS_Interface.Set_RTDSettings_Handle != NULL) AWS_Interface.Set_RTDSettings_Handle(165);
                //     UI_System.Grill_state = State_Grill_KeepWarm;
                // }
                // else if(UI_System.Grill_state == State_Grill_KeepWarm)
                // {
                //     UI_System.Grill_state = State_Grill_Cook;
                // }
            }
            //读取 status 1 (仅供开发板调试用)
            ret = strncmp((char *)received_command, "status", 6); //int strncmp(const char *str1, const char *str2, size_t n) 如果返回值等于 0，则表示 str1 等于 str2。
            if (ret == 0)
            {
                wireless_state.aws_received_command = get_inform; //从AWS获得指令
                for (int i = 0; i < 200; i++)
                {
                    received_command[i] = received_command[i + 7];
                }
                for (int i = 0; i < 50; i++) //清空dest字符串
                {
                    dest[i] = '\0';
                }
                for (int j = 0; j < 1; j++)
                {
                    dest[j] = received_command[j];
                }
                // memcpy(dest, received_command, 1); //复制数字位
                global_grill_state = atoi(dest);   //string 转 int
                ESP_LOGI(TAG, "receive status is %d", global_grill_state);
                //读取command id
                // int i = 0;
                // for (int i = 0; i < 50; i++) //清空command id 字符串
                // {

                //     command_id[i] = '\0';
                // }
                // command_id[0] = '1';
                // while (strncmp(((char *)received_command + i), "\"", 1)) //统计空格前的数量
                // {
                //     i++;
                // }
                // memcpy(command_id, received_command, i);       //复制command id
                // ESP_LOGI(TAG, "command id is %s", command_id); //获取到的command id
            }
            //读取菜单模式
            ret = strncmp((char *)received_command, "setall", 6); //int strncmp(const char *str1, const char *str2, size_t n) 如果返回值等于 0，则表示 str1 等于 str2。
            if (ret == 0)
            {
                ESP_LOGI(TAG, "command \'setall\'");
                for (int i = 0; i < 200; i++)
                {
                    received_command[i] = received_command[i + 7];
                }

                //清空数组
                for (int i = 0; i < 11; i++)
                {
                    for (int j = 0; j < 4; j++)
                    {
                        menu_inform[i][j] = 0;
                    }
                }
                for (int i = 0; i < 50; i++) //清空dest字符串
                {
                    dest[i] = '\0';
                }
                received_command_length -= 7;
                ESP_LOGI(TAG, "received command is %.*s", received_command_length, received_command);
                for (int i = 0; i < 11; i++)
                {
                    for (int j = 0; j < 4; j++)
                    {
                        while (strncmp(((char *)received_command + len), " ", 1) != 0) //统计空格前的数量
                        {
                            if((received_command[len] == 34) || (received_command[len] == 0)) break; //校验结束符"
                            len++;
                            if (len > 10)
                            {
                                ESP_LOGI(TAG, "receive error break 1");
                                continue;
                            }
                        }
                        if(received_command[len] == 0) break; //校验结束符"
                        for (int j = 0; j < len; j++)
                        {
                            dest[j] = received_command[j];
                        }
                        // memcpy(dest, received_command, len); //复制数字位
                        menu_inform[i][j] = atoi(dest);      //string 转 int
                        if(received_command[len] == 34)
                        {
                            CooK_MenuItem_Length = i;
                            menu_inform[i][j] = 0;
                        }
                        ESP_LOGI(TAG, "menu_inform[%d][%d] = %d ", i, j, menu_inform[i][j]);

                        for (int k = 0; k < 200; k++)
                        {
                            received_command[k] = received_command[k + len + 1];
                        }
                        if (len < 10) len = 0;
                        for (int k = 0; k < 50; k++) //清空dest字符串
                        {
                            dest[k] = '\0';
                        }
                        //错误指令
                        if (len > 10)
                        {
                            ESP_LOGI(TAG, "receive error break 2");
                            continue;
                        }
                    }
                    if (len > 10)
                    {
                        for (int i = 0; i < 11; i++)
                        {
                            for (int j = 0; j < 4; j++)
                            {
                                menu_inform[i][j] = 0;
                            }
                        }
                        ESP_LOGI(TAG, "receive error break 3");
                        break;
                    }
                }
                //读取command id
                int i = 0;
                for (int i = 0; i < 50; i++) //清空command id 字符串
                {
                    command_id[i] = '\0';
                }
                command_id[0] = '1';
                while (strncmp(((char *)received_command + i), "\"", 1)) //统计空格前的数量
                {
                    i++;
                }
                for (int j = 0; j < i; j++)
                {
                    command_id[j] = received_command[j];
                }
                // memcpy(command_id, received_command, i);       //复制command id
                ESP_LOGI(TAG, "command id is %s", command_id); //获取到的command id
                 
                if(AWS_Interface.SetMenu_Handle != NULL) AWS_Interface.SetMenu_Handle();
                
            }
            //清空缓存
            received_command_length = 0;
            for (int i = 0; i < 200; i++)
            {
                received_command[i] = '\0';
            }
            // send_result = 1;

            // UI_System.OTA_Check = UI_ON;
        }
        vTaskDelay(1000 / portTICK_RATE_MS);
    }
}
